/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import controller.WorkerCargaData;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

/**
 *
 * @author Enrique
 */
public class CargaData extends javax.swing.JFrame {

    /**
     * Creates new form CargaData
     */
    
    public CargaData() {
        initComponents();
    }


    
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblDirectorio = new javax.swing.JLabel();
        txtDirectorio = new javax.swing.JTextField();
        btnDirectorio = new javax.swing.JButton();
        btnIniciar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Carga de Data Histórica");
        setResizable(false);

        lblDirectorio.setText("Seleccione el directorio de archivos:");

        txtDirectorio.setEditable(false);

        btnDirectorio.setFont(new java.awt.Font("Arial", 0, 11)); // NOI18N
        btnDirectorio.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/buscar.png"))); // NOI18N
        btnDirectorio.setText("Examinar");
        btnDirectorio.setName(""); // NOI18N
        btnDirectorio.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnDirectorioMouseClicked(evt);
            }
        });
        btnDirectorio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDirectorioActionPerformed(evt);
            }
        });

        btnIniciar.setFont(new java.awt.Font("Arial", 0, 11)); // NOI18N
        btnIniciar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/Play.png"))); // NOI18N
        btnIniciar.setText("Iniciar");
        btnIniciar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnIniciarMouseClicked(evt);
            }
        });
        btnIniciar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnIniciarActionPerformed(evt);
            }
        });

        btnCancelar.setFont(new java.awt.Font("Arial", 0, 11)); // NOI18N
        btnCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/Cancelar.png"))); // NOI18N
        btnCancelar.setText("Cancelar");
        btnCancelar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnCancelarMouseClicked(evt);
            }
        });
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(lblDirectorio))
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(txtDirectorio, javax.swing.GroupLayout.PREFERRED_SIZE, 368, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnDirectorio))
            .addGroup(layout.createSequentialGroup()
                .addGap(119, 119, 119)
                .addComponent(btnIniciar, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnCancelar))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addComponent(lblDirectorio)
                .addGap(7, 7, 7)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(9, 9, 9)
                        .addComponent(txtDirectorio, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(btnDirectorio))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnIniciar)
                    .addComponent(btnCancelar)))
        );

        setSize(new java.awt.Dimension(535, 201));
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void btnDirectorioMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnDirectorioMouseClicked

    }//GEN-LAST:event_btnDirectorioMouseClicked

    private void btnDirectorioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDirectorioActionPerformed
        // TODO add your handling code here:
        // creo un JFileChooser, el cual es la ventana para elegir un directorio
        JFileChooser chooser = new JFileChooser();
        chooser.setCurrentDirectory(new java.io.File("."));     // seteo directorio por defecto al abrir la ventana
        chooser.setDialogTitle("Seleccione el directorio de archivos a cargar");    // titulo de la ventana
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);    // para que solamente se puedan seleccionar directorios
        chooser.setAcceptAllFileFilterUsed(false);

        if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {  // cuando el usuario seleccione un directorio y haga click en Abrir
            this.txtDirectorio.setText("" + chooser.getSelectedFile()); // pongo la ruta en el txt
        } else {
            System.out.println("No se selecciono ningun directorio");
        }

        //new SimulacionArchivo().setVisible(true);
    }//GEN-LAST:event_btnDirectorioActionPerformed

    private void btnIniciarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnIniciarMouseClicked

        
    }//GEN-LAST:event_btnIniciarMouseClicked

    private void btnIniciarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnIniciarActionPerformed
        
        if (this.txtDirectorio.getText().isEmpty()){//no se selecciono niguna fila
            
            JOptionPane.showMessageDialog(null, "Debe seleccionar un directorio", "Error",JOptionPane.ERROR_MESSAGE);
            
        }else{
        
            int dialogButton = JOptionPane.YES_NO_OPTION;
            int dialogResult = JOptionPane.showConfirmDialog (null, "¿Desea iniciar la carga de data historica?","Confirmación",dialogButton);
            if(dialogResult == JOptionPane.YES_OPTION){ // si el usuario eligio "SI"
    //           model.Simulacion simu = new model.Simulacion(this.txtDirectorio.getText());  // creo objeto simulacion con el directorio indicado por el usuario
    //           simu.cargaDataHistorica();   // empiezo la carga

                EstadoRegistro ventanaEspera = new EstadoRegistro();
                ventanaEspera.setTitle("Carga de Data Histórica");
                ventanaEspera.getjLabel1().setText("Procesando data histórica y generando ratios. Por favor espere.");
                new WorkerCargaData(this,ventanaEspera,this.txtDirectorio.getText()).execute();
                //this.setFocusableWindowState(false);
                this.dispose();
                ventanaEspera.setVisible(true);


            }
            if (dialogResult==JOptionPane.NO_OPTION)    // si el usuario eligio "NO"
            {
                this.dispose();
            }
        }
    }//GEN-LAST:event_btnIniciarActionPerformed

    private void btnCancelarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnCancelarMouseClicked

        
        new Menu().setVisible(true);
        this.dispose();
    }//GEN-LAST:event_btnCancelarMouseClicked

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_btnCancelarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(CargaData.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(CargaData.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(CargaData.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(CargaData.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new CargaData().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnDirectorio;
    private javax.swing.JButton btnIniciar;
    private javax.swing.JLabel lblDirectorio;
    private javax.swing.JTextField txtDirectorio;
    // End of variables declaration//GEN-END:variables
}
